/** 收集当前window所有框架*/
top.window.getFrame = function( oframe )
{
    if( !oframe.length ) return false;

    for( var a = 0, f; f = oframe[ a ]; a += 1 )
    {
        top.window.FRAMES = top.window.FRAMES || {};

        top.window.FRAMES[ f.name.substring( 2 ) ] = {
            window : frames[  f.name ],
            element : f
        };

    }
    return top.window.FRAMES;
};


/** 超时处理*/
top.window.TIMEOUT = new TimeoutTool();

/** 初始化所有框架*/
top.window.getFrame( document.getElementsByTagName("iframe") );

/** 导航跳转*/
;( function( $ )
{
    var onav = document.getElementById("js-nav");

    var olink = onav.getElementsByTagName("a");

    var defaultPage = onav.getAttribute("data-default") || 0;

    var iFrame = document.getElementById( onav.getAttribute("data-target").substring( 1 ) );

    var oDt = onav.getElementsByTagName("dt")[ 0 ];

    var url = onav.getAttribute("data-url");

    var jumpPage = function( elem )
    {
        var topLoadingWrap = top.document.getElementById("ad-js-init-wrap");

        topLoadingWrap.style.cssText = "";
        topLoadingWrap.style.zIndex = 99;
        topLoadingWrap.style.opacity = 1;

        var sHash = elem.href;

        if( sHash )
        {
            $( olink ).css( "pointer-events", "none");

            sHash = sHash.substring( sHash.indexOf("#") + 1 );

            iFrame.style.opacity = 0;

            iFrame.onload = function()
            {
                var self = this;

                self.style.opacity = 1;

                $( olink ).css( "pointer-events", "");

            }

            iFrame.src = url.replace( "[url]", sHash ) + "?tt=" + new Date().getTime();
        }
    }

    if( onav )
    {
        for( var i = 0, f; f = olink[ i ]; i += 1 )
        {
            f._idx_ = i;

            f.onclick = function()
            {
                if( defaultPage == this._idx_ ) return false;

                jumpPage( this );

                defaultPage = this._idx_;

                sessionStorage.bdataIndex = defaultPage;

                onav._x = this.offsetLeft + this.parentNode.offsetLeft;
                onav._w = this.offsetWidth;

                return false;
            }

            f.onmouseover = function()
            {
                var x = this.offsetLeft + this.parentNode.offsetLeft;
                var w = this.offsetWidth;
                oDt.style.left = x + "px";
                oDt.style.width = w + "px";
            }
        }

        $( onav ).mouseleave( function()
        {
            oDt.style.left = this._x + "px";
            oDt.style.width = this._w + "px";
        })
    }

    if( sessionStorage.bdataIndex )  defaultPage = sessionStorage.bdataIndex;

    jumpPage( olink[ defaultPage ] );
    
    onav._x = olink[ defaultPage ].offsetLeft + olink[ defaultPage ].parentNode.offsetLeft;
    onav._w = olink[ defaultPage ].offsetWidth;
    oDt.style.left = onav._x + "px";
    oDt.style.width = onav._w + "px";

})( jQuery );

/** 意见反馈*/
;( function( $, UPLOAD )
{
    var ocomment = document.getElementById("js-comment");
    var oselectbtn = document.getElementById("js-selectfile");
    var oformmodal = document.getElementById("js-formmodal");
    var ouploadhelp = document.getElementById("uploadhelp");
    var help = ouploadhelp.getAttribute("data-default");
    var ouploadbarwrap = document.getElementById("uploadbarwrap");
    var ocommentsubmit = document.getElementById("js-comment-submit");
    var ouploadbar = document.getElementById("uploadbar");
    var oloading = document.getElementById("ad-js-loading");
    var opmd = document.getElementById("opt0-md");
    var oclose = document.getElementById("closeMode");
    var ofile = document.getElementById("uploadFile");

    var nameItem = [];

    var options = {
        appPath : oformmodal.action,
        readFileSuccess : function( res, file )
        {
            if( this.formatFileSize( file.size, "kb" )[ 0 ] <= 2048 )
            {
                if( /(\.{1}zip)|(\.{1}rar)/.test( file.name ) )
                {
                    ouploadhelp.innerHTML = "名称：<b>" + file.name + "</b>　大小：<b>" + this.formatFileSize( file.size, "mb").join("") + "</b>　<b>文件已准备就绪</b>";
                    ouploadbarwrap.style.display = "block";
                }
                else
                {
                    ocommentsubmit.file = null;
                    toastr.warning("类型错误，请选择RAR或ZIP");
                }
            }
            else {
                ocommentsubmit.file = null;
                toastr.warning("文件太大，必须小于2MB");
            }

            ouploadbar.style.width = 0;
            oloading.style.display = "none";
            oloading.style.opacity = 0;
        },

        onProgress : function( file, loaded, total, startDate )
        {
            var c = ( loaded / total ) * 100;

            var speed = ( function( startDate )
            {
                if( !startDate ) return 0;

                //计算传输网速
                var nowDate = new Date().getTime();
                var x = loaded;
                var y = ( nowDate - startDate) / 1000;

                return x / y;

            })( startDate );

            var name = file.name;
            var currentSize = this.formatFileSize( file.size*c/100, "mb").join("");
            var total = this.formatFileSize( file.size, "mb").join("");
            var speednow = ( speed > 10000 ? "<b> " + this.formatFileSize( speed, "mb")[ 0 ] +  "</b>MB/s" : "<b>" + this.formatFileSize( speed, "kb")[ 0 ] + "</b>KB/S" );

            ouploadhelp.innerHTML = "名称：<b>"+name+"</b>　"+"已上传：<b>"+currentSize+"</b> / "+total+"　"+"传输速度：" + speednow ;

            ouploadbar.style.width = c + "%";
        },

        onUploadStart : function()
        {
            opmd.style.display = "block";
        },

        onUploadComplete : function( file, res )
        {
            opmd.style.display = "none";

            oloading.style.display = "none";
            oloading.style.opacity = 0;

            ouploadbarwrap.style.display = "none";
            ouploadhelp.innerHTML = help;
            ocommentsubmit.file = null;

            if( res )
            {
                toastr.success("意见反馈成功");

                $( oclose ).click();
            }
            else
            {
                toastr.error("意见反馈失败");
            }

            nameItem.push( file.name );
        }
    };

    var Upload = new UPLOAD( options );;

    // 自定义隐藏文件框事件
    var E = document.createEvent("MouseEvents");
    E.initEvent( "click", true, true );
    //ofile.dispatchEvent( E );

    oselectbtn.onclick = function()
    {
        ofile.click();
    }

    ofile.onchange = function( e )
    {
        oloading.style.display = "block";
        oloading.style.opacity = 1;
        oloading.style["-webkit-transition"] =
            oloading.style["-moz-transition"] =
                oloading.style["-o-transition"] =
                    oloading.style["transition"] = "opacity 0.35s ease";

        ocommentsubmit.file = ofile;

        Upload.readFile( e.target.files[ 0 ] );
    };

    ocommentsubmit.onclick = function()
    {
        var $form = $( oformmodal );

        var stextValue = $form.find("input:eq(0)").val();
        var sTextareaValue = $form.find("textarea").val();

        if( !( stextValue && sTextareaValue ) )
        {
            if( !stextValue )
            {
                toastr.warning("请完善反馈标题");

                $form.find("input")[ 0 ].focus();

                return false;
            }

            if( !sTextareaValue )
            {
                toastr.warning("请完善反馈内容");

                $form.find("textarea")[ 0 ].focus();
                return false;
            }
        }

        var sendata = $form.serializeArray();

        var data = {};

        $.each( sendata, function()
        {
            data[ this.name ] = this.value;
        });

        if( this.file )
        {
            Upload.startDate = new Date().getTime();

            Upload.uploadData( this.file.files[ 0 ], data );
        }
        else
        {
            oloading.style.display = "block";
            oloading.style.opacity = 1;
            oloading.style["-webkit-transition"] =
                oloading.style["-moz-transition"] =
                    oloading.style["-o-transition"] =
                        oloading.style["transition"] = "opacity 0.35s ease";

            $.post( oformmodal.action, data, function( res )
            {
                if( res )
                {
                    toastr.success("意见反馈成功");

                    $( oclose ).click();
                }
                else
                {
                    toastr.error("意见反馈失败");
                }

                oloading.style.display = "none";
                oloading.style.opacity = 0;
            })
        }


    }

    ocomment.onclick = function() {
        oformmodal.reset();
        ouploadhelp.innerHTML = help;
    };



})( jQuery, Upload );

/** 最新公告*/
;( function( $ )
{
    $("#js-msg-container").on("click","dl>dt",function(){

        var url = $( this).attr( "data-url" );
        if(!url) return false;
        var title = $(this).find("span").text();
        var time = $(this).find("strong").text();
        var _this = this;

        Handle.loadingShow();

        new Handle.modal( $( _this ).find(".hidBtn") ,{
            dismiss:true
        });


        $.get( url,function( data ){
            $("#announcement-title").html(" [ "+time+" ] "+title);
            $("#announcement-content").html( data );
            Handle.loadingHide();
            $( _this ).find(".hidBtn").click();

            var findOpen = $( _this ).find(".fa-envelope-o");

            if( findOpen.length != 0){

                var badge =  $("#js-badge");

                findOpen.removeClass("fa-envelope-o").addClass("fa-folder-open-o");

                var text = parseInt( badge.text() )-1;

                if( text!=0 ){ badge.text(text); } else { badge.hide(); }
            }
        });
    })
})( jQuery );


/** 右下提示*/
;( function( $ )
{

    var rightBottomLayer = $("#rightbottomLayer");
    rightBottomLayer.addClass("rightbottom");
    setTimeout(function(){
        rightBottomLayer.css("height",165);
    },2000);

    var timer = setTimeout(function(){
        rightBottomLayer.css("height",0);
    },5000);

    rightBottomLayer.on("mouseover",function(){

        clearTimeout( timer );

    }).on("mouseout",function(){

        timer = setTimeout(function(){
            rightBottomLayer.css("height",0);
        },2000);

    }).on("click","samp",function(ev){

        clearTimeout( timer );
        rightBottomLayer.css("height",0);
        ev.stopPropagation();

    }).on("click",".ad-doc-content",function(){
        var tar = $(this).closest(".ad-doc-rightbottomLayer");
        var id = tar.attr("data-id");
        var oMesId = $("#mes-"+id );
        if( oMesId.length == 0 ){

            var badge =  $("#js-badge");

            var text = parseInt( badge.text() )-1;

            if( text!=0 ){ badge.text(text); } else { badge.hide(); }

            var url = tar.attr("data-url");

            var title = tar.attr("data-title");

            var time = tar.attr("data-time");

            var hiddenBtn = $( this ).parent().find(".hidBtn");

            new Handle.modal( hiddenBtn  ,{ dismiss: true });

            $.get(url,function( data ){

                $("#announcement-title").html(" [ "+time+" ] "+title);

                $("#announcement-content").html( data );

                hiddenBtn.click();
            });

        }else{
            oMesId.click();
        }

        rightBottomLayer.css("height",0);
    });

})( jQuery );
